import net.spreadshirt.shop.lib.ProducttypeColor;

/**
 * The ProducttypeColors class stores all available colors
 * of every product type and it provides some methods to handle
 * the color items.
 */
class net.spreadshirt.shop.lib.ProducttypeColors {

	// A list of all color items.
	private var _colors:Array;

	/**
	 * ProducttypeColors Constructor
	 */
	public function ProducttypeColors(items:Array) {
		// Check if items list is containing one or more items.
		if(items.length > 0) {
			addProducttypeColors(items);
		}
	}

	/**
	 * Clears all values.
	 */
	public function clear():Void {
		_colors = new Array();
	}

	/**
	 * Stores a list of color items.
	 * The old list will be overwritten.
	 * 
	 * @param	items	A list of color items.
	 */
	public function addProducttypeColors(items:Array):Void {
		// Clear colors list.
		clear();
		// Adds all items.
		for(var i:Number = 0; i < items.length; i++) {
			addProducttypeColor(items[i]);
		}
	}

	/**
	 * Stores a single color item in the articles list.
	 * 
	 * @param	item	An single color item.
	 */
	public function addProducttypeColor(item:Object):Void {
		// Only adds the item if there's no other item
		// with the same color ID already stored.
		if(!isExisting(item)){
			var color:ProducttypeColor = new ProducttypeColor(item);
			_colors.push(color);
		}
	}

	/**
	 * Returns a single color item with the requested product type color ID.
	 * 
	 * @param	id	The product type color ID.
	 * @return	A product type color item.
	 */
	public function getProducttypeColor(id:Number):ProducttypeColor {
		var color:ProducttypeColor;
		for(var i:Number = 0; i < _colors.length; i++) {
			if(id == _colors[i].producttype_color_id) {
				color = _colors[i];
			}
		}
		return color;
	}

	/**
	 * Checks if the color item is already stored.
	 * 
	 * @param	item	A single color item.
	 * 
	 * @return	true or false
	 */
	private function isExisting(item:Object):Boolean {
		var found:Boolean = false;
		var id:Number = item.producttype_color_id;
		for(var i:Number = 0; i < _colors.length; i++) {
			if(id == _colors[i].producttype_color_id) {
				found = true;
				break;
			}
		}
		return found;
	}

}